﻿using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace Dotnet6.Web.DbEntitys
{
    [Table("UserInfo")]
    public class UserInfo
    {
        [Key]

        public string Id { get; set; }

        public string Name { get; set; }

        public string Account { get; set; }

        public string Password { get; set; }

        public int Age { get; set; }

        public string Phone { get; set; }

        public DateTime CreateTime { get; set; }

        /// <summary>
        /// 默认构造函数，给EF用
        /// </summary>
        public UserInfo()
        {

        }

        /// <summary>
        /// 业务构造函数
        /// </summary>
        public UserInfo(string name, string account, string password, int age, string phone)
        {
            Id = Guid.NewGuid().ToString("N");
            CreateTime = DateTime.Now;
            Save(name, account, password, age, phone);
        }

        public void Edit(string name, string account, string password, int age, string phone)
        {
            Save(name, account, password, age, phone);
        }

        /// <summary>
        /// 给模型赋值，并验证
        /// </summary>
        private void Save(string name, string account, string password, int age, string phone)
        {
            Name = name;
            Account = account;
            Password = password;
            Age = age;
            Phone = phone;
            Check();
        }

        /// <summary>
        ///  验证
        /// </summary>
        public void Check()
        {
            if (string.IsNullOrWhiteSpace(Name))
                throw new Exception("姓名不可为空！");
            if (string.IsNullOrWhiteSpace(Account))
                throw new Exception("账号不可为空！");
            if (string.IsNullOrWhiteSpace(Password))
                throw new Exception("密码不可为空！");
            if (string.IsNullOrWhiteSpace(Phone))
                throw new Exception("手机号不可为空！");
        }
    }
}
